home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10150 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  59 lines

  1. Path: news.chalmers.se!usenet
  2. From: Eivind Nordby <Eivind.Nordby@hks.se>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Using cin like sscanf()?
  5. Date: Wed, 06 Mar 1996 13:37:36 +0100
  6. Organization: University of Karlstad, Dept of Computer Science
  7. Message-ID: <313D8710.1CFB@hks.se>
  8. References: <4gnudd$dd4@daily-planet.nodak.edu>
  9. NNTP-Posting-Host: eivind.cs.hks.se
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; AIX 2)
  14. CC: ishaq@plains.nodak.edu
  15.  
  16. Jeffro wrote:
  17. > Say I input a string into string:
  18. > char string[] = "This is a character string.";
  19. > char buffer[24];
  20. > //cin.read_from(string)
  21. > while (cin >> buffer) cout << buffer;    //Outputs string
  22. > //cin.read_from(stdin)
  23. > This should be similar to:
  24. > while (sscanf(string, "%s", &buffer)) cout << buffer;
  25. > Any ideas?
  26. > Thanks!
  27. >         -jeff
  28. > --
  29. >  .,;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.
  30. >  - Jeff Ishaq ishaq@plains.nodak.edu               Computer Science major -
  31. >  - at North Dakota State University in Fargo, ND.  Drop me a line, eh?    -
  32.  
  33. C++ has a class istrstream for this purpose. You need to declare a istrstream
  34. variable on the input string string. With your example:
  35.  
  36. #include <strstream.h>
  37.    :
  38. {
  39.   istrstream sin(string);
  40.   while (sin >> buffer) cout << buffer;
  41. }
  42.  
  43. This program fragment will display Thisisacharacterstring.
  44. The braces make the sin variable vanish once it has done its job.
  45.  
  46. Good luck
  47. Eivind
  48. -- 
  49. _____________________________________________________________________________
  50. Dept of Computer Science * University of Karlstad * 651 88  Karlstad * Sweden
  51. Eivind J. Nordby * Eivind.Nordby@hks.se; http://f-server.cs.hks.se:80/~eivind/
  52.     Voice: +46-(0)54-83 81 53 (mo, we, fr) * Fax: +46-(0)54-83 84 46
  53.